package de.deliancourt.jarvis.implementations;

import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;

import javax.media.j3d.Appearance;
import javax.media.j3d.Geometry;
import javax.media.j3d.ImageComponent2D;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Texture;
import javax.media.j3d.Texture2D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.TransparencyAttributes;
import javax.vecmath.Point2d;
import javax.vecmath.Point3d;
import javax.vecmath.TexCoord2f;

import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.image.TextureLoader;

public class BaseWindow extends Primitive {

	private WindowSide _Front;
	private WindowSide _Back;
	private WindowSide _Bottom;
	private WindowSide _Top;
	private WindowSide _Left;
	private WindowSide _Right;
	private Point3d _TopLeft; 
	
	private TexCoord2f[] TopLeft={
			new TexCoord2f(0.19f,1.0f),
			new TexCoord2f(0.19f,0.92f),
			new TexCoord2f(0.29f,0.92f),
			new TexCoord2f(0.29f,0.92f),
			new TexCoord2f(0.29f,1.0f),
			new TexCoord2f(0.19f,1.0f),
		};
	
	private TexCoord2f[] TopRight={
			new TexCoord2f(0.09f,1.0f),
			new TexCoord2f(0.09f,0.92f),
			new TexCoord2f(0.19f,0.92f),
			new TexCoord2f(0.19f,0.92f),
			new TexCoord2f(0.19f,1.0f),
			new TexCoord2f(0.09f,1.0f),
		};
	
	private TexCoord2f[] BottomLeft={
			new TexCoord2f(0.0820f,0.9150f),
			new TexCoord2f(0.0820f,0.8350f),
			new TexCoord2f(0.1875f,0.8350f),
			new TexCoord2f(0.1875f,0.8350f),
			new TexCoord2f(0.1875f,0.9150f),
			new TexCoord2f(0.0820f,0.9150f),
		};
	
	private TexCoord2f[] BottomRight={
			new TexCoord2f(0.20f,0.9150f),
			new TexCoord2f(0.20f,0.8350f),
			new TexCoord2f(0.2985f,0.8350f),
			new TexCoord2f(0.2985f,0.8350f),
			new TexCoord2f(0.2985f,0.9150f),
			new TexCoord2f(0.20f,0.9150f),
		};
	private TexCoord2f[] TopBorder={
			new TexCoord2f(0.30f,1.0f),
			new TexCoord2f(0.30f,0.92f),
			new TexCoord2f(0.33f,0.92f),
			new TexCoord2f(0.33f,0.92f),
			new TexCoord2f(0.33f,1.0f),
			new TexCoord2f(0.30f,1.0f),
		};
	
	private TexCoord2f[] BottomBorder={
			new TexCoord2f(0.34f,1.0f),
			new TexCoord2f(0.34f,0.9210f),
			new TexCoord2f(0.36f,0.9210f),
			new TexCoord2f(0.36f,0.9210f),
			new TexCoord2f(0.36f,1.0f),
			new TexCoord2f(0.34f,1.0f),
		};
	
	private TexCoord2f[] LeftBorder={
			new TexCoord2f(0.3675f,1.0f),
			new TexCoord2f(0.3675f,0.97f),
			new TexCoord2f(0.47f,0.97f),
			new TexCoord2f(0.47f,0.97f),
			new TexCoord2f(0.47f,1.0f),
			new TexCoord2f(0.3675f,1.0f),
		};
	
	private TexCoord2f[] RightBorder={
			new TexCoord2f(0.37f,0.95f),
			new TexCoord2f(0.37f,0.92f),
			new TexCoord2f(0.4765f,0.92f),
			new TexCoord2f(0.4765f,0.92f),
			new TexCoord2f(0.4765f,0.95f),
			new TexCoord2f(0.37f,0.95f)
	};
	
	private TexCoord2f[] Middle={
			new TexCoord2f(0.30f,0.9160f),
			new TexCoord2f(0.30f,0.8375f),
			new TexCoord2f(0.3925f,0.8375f),
			new TexCoord2f(0.3925f,0.8375f),
			new TexCoord2f(0.3925f,0.9160f),
			new TexCoord2f(0.30f,0.9160f)
	};
	
	public BaseWindow(Point3d TopLeft, double Width, double Height, double Depth) {
		_TopLeft = TopLeft;
		_Front = new WindowSide(TopLeft, new Point2d(Width,Height),Tile.XY,8,6);
		_Back = new WindowSide(new Point3d(TopLeft.x,TopLeft.y,TopLeft.z+Depth), new Point2d(Width,Height),Tile.XY,8,6);
		_Bottom = new WindowSide(new Point3d(TopLeft.x,TopLeft.y-Height,TopLeft.z), new Point2d(Width,-Depth),Tile.XZ,1,6);
		_Top = new WindowSide(TopLeft, new Point2d(Width,-Depth),Tile.XZ,1,6);
		_Left = new WindowSide(TopLeft, new Point2d(Height,TopLeft.z-Depth),Tile.YZ,8,1);
		_Right = new WindowSide(new Point3d(TopLeft.x+Width,TopLeft.y,TopLeft.z), new Point2d(Height,TopLeft.z-Depth),Tile.YZ,8,1);
		
		//addChild(_Front.getShape(Pos));
	    //addChild(_Back);
		//addChild(_Bottom);
		//addChild(_Top);
		//addChild(_Left);
		//addChild(_Right);
		
		// System.out.println("Num Verts of Front: " +((Primitive)getChild(0)).numChildren()*6);
		AssembleChilds();
		GenereteTexCoords();
		SetTexture();
		ResetOnSphere();
		
	}
	public BaseWindow() {
		this(new Point3d(-5,2.5,-30),10,5,0.5);
	}
	
	private void AssembleChilds(){
		//for ( TransformGroup g : _Front.getAlltGroups()) addChild(g);
		for ( Shape3D g : _Front.getAllShapes()) addChild(g);
		
		/*for ( Shape3D s : _Back.getAllShapes()) addChild(s);
		for ( Shape3D s : _Bottom.getAllShapes()) addChild(s);
		for ( Shape3D s : _Top.getAllShapes()) addChild(s);
		for ( Shape3D s : _Left.getAllShapes()) addChild(s);
		for ( Shape3D s : _Right.getAllShapes()) addChild(s);
		*/
	}
	public void GenereteTexCoords(){
	 	 
	 _Front.GetTile(0,1).setTexCoordinates(0, 0, TopLeft);
	 _Front.GetTile(0,2).setTexCoordinates(0, 0, TopBorder);
	 _Front.GetTile(0,3).setTexCoordinates(0, 0, TopBorder);
	 _Front.GetTile(0,4).setTexCoordinates(0, 0, TopBorder);
	 _Front.GetTile(0,5).setTexCoordinates(0, 0, TopBorder);
	 _Front.GetTile(0,6).setTexCoordinates(0, 0, TopRight);
	 
	 _Front.GetTile(7,1).setTexCoordinates(0, 0, BottomLeft);
	 _Front.GetTile(7,2).setTexCoordinates(0, 0, BottomBorder);
	 _Front.GetTile(7,3).setTexCoordinates(0, 0, BottomBorder);
	 _Front.GetTile(7,4).setTexCoordinates(0, 0, BottomBorder);
	 _Front.GetTile(7,5).setTexCoordinates(0, 0, BottomBorder);
	 _Front.GetTile(7,6).setTexCoordinates(0, 0, BottomRight);
	 
	 _Front.GetTile(1,1).setTexCoordinates(0, 0, LeftBorder);
	 _Front.GetTile(2,1).setTexCoordinates(0, 0, LeftBorder);
	 _Front.GetTile(3,1).setTexCoordinates(0, 0, LeftBorder);
	 _Front.GetTile(4,1).setTexCoordinates(0, 0, LeftBorder);
	 _Front.GetTile(5,1).setTexCoordinates(0, 0, LeftBorder);
	 _Front.GetTile(6,1).setTexCoordinates(0, 0, LeftBorder);
	 
	 _Front.GetTile(1,6).setTexCoordinates(0, 0, RightBorder);
	 _Front.GetTile(2,6).setTexCoordinates(0, 0, RightBorder);
	 _Front.GetTile(3,6).setTexCoordinates(0, 0, RightBorder);
	 _Front.GetTile(4,6).setTexCoordinates(0, 0, RightBorder);
	 _Front.GetTile(5,6).setTexCoordinates(0, 0, RightBorder);
	 _Front.GetTile(6,6).setTexCoordinates(0, 0, RightBorder);

	 for (Integer Row=1;Row<7;Row++){
			for(Integer Column=2;Column<6;Column++){
				_Front.GetTile(Row,Column).setTexCoordinates(0, 0, Middle);
			}
	 }
	}
	public void SetTexture(){
		   
		Appearance a = new Appearance();
		TransparencyAttributes ta = new TransparencyAttributes();
		PolygonAttributes pa = new PolygonAttributes();
		
		   URL imgURL = Thread.currentThread().getContextClassLoader().getResource("de/deliancourt/jarvis/media/textures/WindowTexture.png");
		   Texture img = new TextureLoader(imgURL,null).getTexture();
		    ImageComponent2D myImage = (ImageComponent2D) img.getImage(0);
	        Texture2D texture = new Texture2D();
	        texture = new Texture2D(Texture2D.BASE_LEVEL, Texture2D.RGBA,512,512);
	        texture.setImage(0, myImage);
	        texture.setEnable(true);
	          a.setTexture(texture);
	          ta.setTransparencyMode (TransparencyAttributes.FASTEST);
	          pa.setCullFace(PolygonAttributes.CULL_NONE);
	          ta.setTransparency(1.0f);
	          a.setTransparencyAttributes(ta);
	          a.setPolygonAttributes(pa);
	          _Front.setAppearance(a);
	}
	
	@Override
	public Appearance getAppearance(int partId) {
		if(partId<0 || partId>5) return null; 
		return ((Shape3D)getChild(partId)).getAppearance();
		
	}
	
	public void ResetOnSphere() {
		 Transform3D rotate = new Transform3D();
		 Transform3D tmp = new Transform3D();
		 
		 
		for (Tile ti : _Front.getAllTiles()){
			
			for (Integer i=0;i<6;i++){
				ti.SetCoords(i, new Point3d(UISphere.PointOnSphere(ti.GetCoords(i).x, ti.GetCoords(i).y, ti.GetCoords(i).z)));
			}
			
			//for ( TransformGroup g : _Front.getAlltGroups()) g.setTransform(tmp);
			
			//_Front.gettGroup(5).setTransform(tmp);
		}
		
	}
	@Override
	public Shape3D getShape(int partid) {
		if(partid<0 || partid>5) return null; 
		return ((Shape3D)getChild(partid));
		
	}

	@Override
	public void setAppearance(Appearance ap) {
		_Front.setAppearance(ap);
		_Back.setAppearance(ap);
		_Bottom.setAppearance(ap);
		_Top.setAppearance(ap);
		_Left.setAppearance(ap);
		_Right.setAppearance(ap);
	}
	

}
